package com.itheima.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.NursingPlan;
import com.itheima.domain.dto.NursingPlanDto;
import com.itheima.domain.dto.NursingProjectPlanDto;
import com.itheima.mapper.NursingPlanMapper;
import com.itheima.mapper.NursingProjectPlanMapper;
import com.itheima.service.NursingPlanService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Transactional
    @Override
    public boolean add(NursingPlanDto nursingPlanDto) {
        System.out.println(nursingPlanDto);
        // 先添加计划表信息，以便添加中间表时获取计划id
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        nursingPlan.setCreateTime(LocalDateTime.now());
        int isSuccess1 = nursingPlanMapper.insert(nursingPlan);
        // 批量添加中间表信息
        Integer planId = nursingPlan.getId();
        List<NursingProjectPlanDto> projectPlanDtoList = nursingPlanDto.getProjectPlanList();
        int isSuccess2 = nursingProjectPlanMapper.batchInsert(projectPlanDtoList,planId);

        if (isSuccess1 > 0 && isSuccess2 > 0) {
            return true;
        } else {
            return false;
        }
    }
}
